package com.ruoyi.assets.queue.service;

import com.ruoyi.assets.dto.StateVisitDTO;
import com.ruoyi.assets.service.IStateVisitService;
import com.ruoyi.common.utils.Threads;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;


@Slf4j
@Service
public class MessageConsumer {

    @Autowired
    private RedisTemplate redisTemplate;
    @Resource
    private IStateVisitService articleVisitService;

    /**
     * 从队列中获取消息
     *
     * @param queueName 队列名称
     */
    public void consumeMessage(String queueName) {
        while (true) {
            // 阻塞式获取消息，超时时间为 0（无限等待）
            StateVisitDTO dto = (StateVisitDTO) redisTemplate.opsForList().rightPop(queueName);
            if (dto != null) {
                System.out.println("接收到消息: " + dto);
                // 处理消息
                articleVisitService.handleFromConsumeMessage(dto);
            }
        }
    }

}